package com.etc.traval.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.etc.traval.biz.ComplaintBiz;
import com.etc.traval.biz.SellerBiz;
import com.etc.traval.biz.StrategyBiz;
import com.etc.traval.biz.UserBiz;
import com.etc.traval.bizimpl.ComplaintBizImpl;
import com.etc.traval.bizimpl.SellerBizImpl;
import com.etc.traval.bizimpl.StrategyBizImpl;
import com.etc.traval.bizimpl.UserBizImpl;
import com.etc.traval.pojo.SellerPojo;
import com.etc.traval.pojo.UserPojo;
import com.etc.traval.util.JDBCUtil;
import com.etc.travel.entity.Complaint;
import com.etc.travel.entity.Seller;
import com.etc.travel.entity.Strategy;
import com.etc.travel.entity.User;
import com.google.gson.Gson;
import com.mysql.jdbc.Util;

public class AdminServlet extends HttpServlet {

	/**
		 * Constructor of the object.
		 */
	public AdminServlet() {
		super();
	}

	/**
		 * Destruction of the servlet. <br>
		 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
		 * The doGet method of the servlet. <br>
		 *
		 * This method is called when a form has its tag value method equals to get.
		 * 
		 * @param request the request send by the client to the server
		 * @param response the response send by the server to the client
		 * @throws ServletException if an error occurred
		 * @throws IOException if an error occurred
		 */
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		doPost(request, response);
	}

	/**
		 * The doPost method of the servlet. <br>
		 *
		 * This method is called when a form has its tag value method equals to post.
		 * 
		 * @param request the request send by the client to the server
		 * @param response the response send by the server to the client
		 * @throws ServletException if an error occurred
		 * @throws IOException if an error occurred
		 */
	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		String flag = null;
		if(request.getParameter("flag")!=null){
			flag = request.getParameter("flag");
		}
		UserBiz userBiz = new UserBizImpl();
		SellerBiz sellerBiz = new SellerBizImpl();
		StrategyBiz strategyBiz = new StrategyBizImpl();
		ComplaintBiz complaintBiz = new ComplaintBizImpl();
		if(flag.equals("strategyinfo")){
			int strategyid=Integer.valueOf(request.getParameter("strategyid"));
			Strategy strategy=strategyBiz.querySTrategyById(strategyid);
			request.setAttribute("strategyinfo", strategy);
			request.getRequestDispatcher("adminstrategy.jsp").forward(request, response);

		}else if(flag.equals("setuserinfo")){
			List<User> list= userBiz.queryalluser();
			Gson gson=new Gson();
		    String jsonString= gson.toJson(list);
		    response.setContentType("text/html;charset=utf-8");;
		    PrintWriter out = response.getWriter();
		    out.print(jsonString);
		    out.flush();
		    out.close();  
		}else if(flag.equals("setsellerinfo")){
			List<Seller> list= sellerBiz.queryAll(1, 999999999);
			Gson gson=new Gson();
		    String jsonString= gson.toJson(list);
		    response.setContentType("text/html;charset=utf-8");;
		    PrintWriter out = response.getWriter();
		    out.print(jsonString);
		    out.flush();
		    out.close();  
		}else if (flag.equals("setstrategyinfo")) {
			List<Strategy> list = strategyBiz.queryAllStrategyByAdmin(1, 99999);
			Gson gson=new Gson();
		    String jsonString= gson.toJson(list);
		    response.setContentType("text/html;charset=utf-8");;
		    PrintWriter out = response.getWriter();
		    out.print(jsonString);
		    out.flush();
		    out.close(); 
		}else if (flag.equals("setcomplaintinfo")) {
			List<Complaint> list = complaintBiz.queryCompByPage(99999999, 1);
			Gson gson=new Gson();
		    String jsonString= gson.toJson(list);
		    response.setContentType("text/html;charset=utf-8");;
		    PrintWriter out = response.getWriter();
		    out.print(jsonString);
		    out.flush();
		    out.close(); 
		}else if (flag.equals("changecomplaint")) {
			int compid=Integer.valueOf(request.getParameter("complaintid"));
			if(complaintBiz.updateCompStatusById(compid)){
				List<Complaint> list = complaintBiz.queryCompByPage(99999999, 1);
				Gson gson=new Gson();
			    String jsonString= gson.toJson(list);
			    response.setContentType("text/html;charset=utf-8");;
			    PrintWriter out = response.getWriter();
			    out.print(jsonString);
			    out.flush();
			    out.close();
			}else {
				String yesorno="修改失败";
				Gson gson=new Gson();
			    String jsonString= gson.toJson(yesorno);
			    response.setContentType("text/html;charset=utf-8");;
			    PrintWriter out = response.getWriter();
			    out.print(jsonString);
			    out.flush();
			    out.close(); 
			}			
		}else if (flag.equals("getonecomplaint")) {
			int compid=Integer.valueOf(request.getParameter("complaintid"));
			Complaint complaint = complaintBiz.queryComplaintById(compid);
			Gson gson=new Gson();
		    String jsonString= gson.toJson(complaint);
		    response.setContentType("text/html;charset=utf-8");;
		    PrintWriter out = response.getWriter();
		    out.print(jsonString);
		    out.flush();
		    out.close();
		}else if(flag.equals("getuserpojo")){
			JDBCUtil util = new JDBCUtil();
			UserPojo userPojo = new UserPojo();
			ResultSet rs1=util.query("select count(*) as getnum from t_user where user_register like '%1月%'");
			ResultSet rs2=util.query("select count(*) as getnum from t_user where user_register like '%2月%'");
			ResultSet rs3=util.query("select count(*) as getnum from t_user where user_register like '%3月%'");
			ResultSet rs4=util.query("select count(*) as getnum from t_user where user_register like '%4月%'");
			ResultSet rs5=util.query("select count(*) as getnum from t_user where user_register like '%5月%'");
			ResultSet rs6=util.query("select count(*) as getnum from t_user where user_register like '%6月%'");
			try {
				if(rs1.next()){
					userPojo.setJan(rs1.getInt("getnum"));
				}else {
					userPojo.setJan(0);
				}
				if(rs2.next()){
					userPojo.setFed(rs2.getInt("getnum"));
				}else {
					userPojo.setFed(0);
				}
				if(rs3.next()){
					userPojo.setMar(rs3.getInt("getnum"));
				}else {
					userPojo.setJan(0);
				}
				if(rs4.next()){
					userPojo.setApr(rs4.getInt("getnum"));
				}else {
					userPojo.setApr(0);
				}
				if(rs5.next()){
					userPojo.setMay(rs5.getInt("getnum"));
				}else {
					userPojo.setMay(0);
				}
				if(rs6.next()){
					userPojo.setJun(rs6.getInt("getnum"));
				}else {
					userPojo.setJun(0);
				}
			}catch (SQLException e) {
				e.printStackTrace();
			}finally {
				util.closeAll();
			}
			Gson gson=new Gson();
		    String jsonString= gson.toJson(userPojo);
		    response.setContentType("text/html;charset=utf-8");;
		    PrintWriter out = response.getWriter();
		    out.print(jsonString);
		    out.flush();
		    out.close();
		}else if (flag.equals("getsellerpojo")) {
			JDBCUtil util = new JDBCUtil();
			SellerPojo sellerPojo = new SellerPojo();
			ResultSet rs1=util.query("select count(*) as getnum from t_seller where seller_register like '%1月%'");
			ResultSet rs2=util.query("select count(*) as getnum from t_seller where seller_register like '%2月%'");
			ResultSet rs3=util.query("select count(*) as getnum from t_seller where seller_register like '%3月%'");
			ResultSet rs4=util.query("select count(*) as getnum from t_seller where seller_register like '%4月%'");
			ResultSet rs5=util.query("select count(*) as getnum from t_seller where seller_register like '%5月%'");
			ResultSet rs6=util.query("select count(*) as getnum from t_seller where seller_register like '%6月%'");
			try {
				if(rs1.next()){
					sellerPojo.setJan(rs1.getInt("getnum"));
				}else {
					sellerPojo.setJan(0);
				}
				if(rs2.next()){
					sellerPojo.setFed(rs2.getInt("getnum"));
				}else {
					sellerPojo.setFed(0);
				}
				if(rs3.next()){
					sellerPojo.setMar(rs3.getInt("getnum"));
				}else {
					sellerPojo.setMar(0);
				}
				if(rs4.next()){
					sellerPojo.setApr(rs4.getInt("getnum"));
				}else {
					sellerPojo.setApr(0);
				}
				if(rs5.next()){
					sellerPojo.setMay(rs5.getInt("getnum"));
				}else {
					sellerPojo.setMay(0);
				}
				if(rs6.next()){
					sellerPojo.setJun(rs6.getInt("getnum"));
				}else {
					sellerPojo.setJun(0);
				}
			}catch (SQLException e) {
				e.printStackTrace();
			}finally {
				util.closeAll();
			}
			Gson gson=new Gson();
		    String jsonString= gson.toJson(sellerPojo);
		    response.setContentType("text/html;charset=utf-8");;
		    PrintWriter out = response.getWriter();
		    out.print(jsonString);
		    out.flush();
		    out.close();
		}else if (flag.equals("endadmin")) {
			request.getSession().invalidate();
			response.sendRedirect("index.jsp");
		}else if (flag.equals("thestrategyisok")) {
			int strategyid=Integer.valueOf(request.getParameter("strategyid"));
			strategyBiz.updatestrategyok(strategyid);
			PrintWriter out = response.getWriter();
			out.print("<script>window.close();</script>");
			
		}
		
		
	}

	/**
		 * Initialization of the servlet. <br>
		 *
		 * @throws ServletException if an error occurs
		 */
	public void init() throws ServletException {
		// Put your code here
	}

}
